package com.example.arithmeticleetcode.leetcode.october;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-11-02 09:17
 **/
public class Demo21 {

    public static void main(String[] args) {
        //[[78],[4,64],[91]]
        int[][] pieces1 = {{78},{64,4},{91}};
        int[] arr1 = {91,4,64,78};
        System.out.println(canFormArray(arr1, pieces1));

    }

    public static boolean canFormArray(int[] arr, int[][] pieces) {
        List<Integer> arrList = new ArrayList<>();
        List<Integer> arrList2 = new ArrayList<>();
        for (int i = 0, len = arr.length; i < len; i++) {
            arrList.add(arr[i]);
            arrList2.add(arr[i]);
        }
        for (int i = 0, len = pieces.length; i < len; i++) {
            for (int j = 0, len1 = pieces[i].length; j < len1; j++) {
                arrList.remove(arrList.indexOf(pieces[i][j]));
            }

            if (pieces[i].length > 1) {
                int index = arrList2.indexOf(pieces[i][0]);
                int tempIndex = 0;
                for (int arrIndex = index, arrLen = index + pieces[i].length; arrIndex < arrLen; arrIndex++) {
                    if (arrList2.get(index) != pieces[i][tempIndex++]) {
                        return false;
                    }
                }
            }
        }
        if (!arrList.isEmpty()) {
            return false;
        }
        return true;
    }
}
